/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef COMMON_H
#define COMMON_H

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>
#include <semaphore.h>
#include <unistd.h>
#include <securec.h>

#include "nativetoken_kit.h"
#include "softbus_common.h"
#include "token_setproc.h"

#ifdef __cplusplus
extern "C" {
#endif

#define MAC_SIZE 6
#define PKG_NAME "com.huawei.dmsdp"

#define MAC_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define MAC_ADDR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]

inline void WaitInputEnter(const char *info)
{
    printf("==>%s", info);
    fflush(stdout);
    fflush(stdin);
    while (getchar() != '\n'){}
    while (getchar() != '\n'){}
}

inline int32_t GetInputNumber(const char *info)
{
    int32_t num;
    printf("==>%s", info);
    fflush(stdout);
    fflush(stdin);
    (void)scanf_s("%d", &num, sizeof(num));
    return num;
}

inline void GetInputString(const char *info, char *str, uint32_t size)
{
    printf("==>%s", info);
    fflush(stdout);
    fflush(stdin);
    if (scanf_s("%s", str, size) < 0) {
        printf("[ERR]invalid input!\n");
    }
}

inline void AddPermission()
{
    uint64_t tokenId;
    const char *perms[2];
    perms[0] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
    perms[1] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
    NativeTokenInfoParams infoInstance = {
        .dcapsNum = 0,
        .permsNum = 2,
        .aclsNum = 0,
        .dcaps = NULL,
        .perms = perms,
        .acls = NULL,
        .processName = "dsoftbus_service",
        .aplStr = "system_core",
    };
    tokenId = GetAccessTokenId(&infoInstance);
    SetSelfTokenID(tokenId);
}

enum LogLevel{
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
    FATAL,
    LOG_LEVEL_BUTT
};
int32_t Logf(enum LogLevel level, const char *format, ...);

#ifdef __cplusplus
}
#endif
#endif